home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Get1IndString / Get1IndString.sit / Get1IndString.cp next >
Text File  |  1995-06-28  |  3KB  |  117 lines

  1. //    Get1IndString.cp -- get a specific string from a STR# resource
  2. //        this is the same as the Toolbox function GetIndString except only the
  3. //        first (current) resource file is searched.
  4. //
  5. //        Also includes utility functions for counting the number of strings in
  6. //        an STR# resource.
  7. //
  8. //        Use the C++ compiler to compile this code. You may call the functions from
  9. //        either C or C++.
  10. //
  11. //        Copyright ⌐ 1995 Gregory W. Jorgensen, All Rights Reserved
  12. //        May be freely distributed and used if this copyright notice is retained
  13. //
  14. //        NOTE: this function does not change the state of the STR# resource,
  15. //        nor does it release or purge the STR# resource when done. If you don't
  16. //        want the STR# resource to sit around in the heap mark it purgeable
  17. //        in the resource attributes (with ResEdit or Resorcerer), or do this:
  18. //
  19. //            Handle h = Get1Resource('STR#', resid);
  20. //            if ( h ) HPurge(h);
  21.  
  22. #include "Get1IndString.h"
  23.  
  24. //    this struct maps the STR# resource structure, sort of
  25. struct strlist {
  26.     short            count;
  27.     unsigned char    data[1];
  28. };
  29.  
  30. //    Get1IndString -- get a specified string from a STR# resource
  31. //        if the resource is not found, or the specified string is not in the STR#
  32. //        resource, the string is returned empty (0 in the first byte)
  33.  
  34. void Get1IndString(Str255 thestring, short resid, short index)
  35. {
  36.     thestring[0] = 0;
  37.     
  38.     //    get the resource
  39.     Handle h = Get1Resource('STR#', resid);
  40.     if ( h ) {
  41.         //    map handle to strlist structure
  42.         strlist* sp = (strlist*)(*h);
  43.         //    if index is in range, scan through stringlist for requested string
  44.         if ( index > 0 && index <= sp->count ) {
  45.             unsigned char* s;
  46.             for ( s = sp->data; --index; s += (s[0] + 1) ) continue;
  47.             
  48.             //    copy the found string into the Str255 parameter
  49.             //    the extra byte copied because of the <= comparison is the length
  50.             for ( short i = 0; i <= s[0]; ++i )
  51.                 thestring[i] = s[i];
  52.         }
  53.     }
  54. }
  55.  
  56. //    get1indstring -- C glue for Get1IndString
  57. //        same thing as Get1IndString except returns a C string
  58. //        the passed string must be a char array of at least 256 bytes
  59. //        (no wimpy C glue that just calls the Pascal function here)
  60.  
  61. char* get1indstring(char* thestring, short resid, short index)
  62. {
  63.     if ( thestring ) {
  64.         thestring[0] = 0;
  65.         
  66.         //    get the resource
  67.         Handle h = Get1Resource('STR#', resid);
  68.         if ( h ) {
  69.             //    map handle to strlist structure
  70.             strlist* sp = (strlist*)(*h);
  71.             //    if index is in range, scan through stringlist for requested string
  72.             if ( index > 0 && index <= sp->count ) {
  73.                 unsigned char* s;
  74.                 for ( s = sp->data; --index; s += (s[0] + 1) ) continue;
  75.                 
  76.                 //    copy the found string into the Str255 parameter
  77.                 for ( short i = 0; i < s[0]; ++i )
  78.                     thestring[i] = s[i+1];
  79.                 //    terminate the C string
  80.                 thestring[ s[0] ] = 0;
  81.             }
  82.         }
  83.     }
  84.     
  85.     return thestring;
  86. }
  87.  
  88. //    CountStrings -- return number of strings in an STR# resource
  89.  
  90. short CountStrings(short resid)
  91. {
  92.     short count = 0;
  93.     
  94.     Handle h = GetResource('STR#', resid);
  95.     if ( h ) {
  96.         strlist* sp = (strlist*)(*h);
  97.         count = sp->count;
  98.     }
  99.     
  100.     return count;
  101. }
  102.  
  103. //    Count1Strings -- return number of strings in an STR# resource (1-deep search)
  104.  
  105. short Count1Strings(short resid)
  106. {
  107.     short count = 0;
  108.     
  109.     Handle h = Get1Resource('STR#', resid);
  110.     if ( h ) {
  111.         strlist* sp = (strlist*)(*h);
  112.         count = sp->count;
  113.     }
  114.     
  115.     return count;
  116. }
  117.